#!/bin/bash

cd /home/qie_cwar_1/code/run

PATH_ID=1
SET_ID=1

export LD_LIBRARY_PATH=/home/qie_cwar_${PATH_ID}/code/bin:$LD_LIBRARY_PATH
export PATH=/home/qie_cwar_${PATH_ID}/code/bin:$PATH

host=`cut -d_ -f2 <<< $HOSTNAME`
ulimit -c unlimited
ulimit -n 102400

cd /home/qie_cwar_${PATH_ID}/code/run

mv dbsvrd  ${SET_ID}dbsvrd  &>/dev/null
mv logsvrd ${SET_ID}logsvrd &>/dev/null
mv trfsvrd ${SET_ID}trfsvrd &>/dev/null
mv gmsvrd  ${SET_ID}gmsvrd  &>/dev/null

rm -rf ./cid/5.${SET_ID}.cid
rm -rf ./cid/6.${SET_ID}.cid
rm -rf ./cid/7.${SET_ID}.cid
rm -rf ./cid/9.${SET_ID}.cid

rm -rf ./tid/5.${SET_ID}.tid
rm -rf ./tid/6.${SET_ID}.tid
rm -rf ./tid/7.${SET_ID}.tid
rm -rf ./tid/9.${SET_ID}.tid

rm -rf ./${SET_ID}dbsvrd/core*
rm -rf ./${SET_ID}dbsvrd/*.MMAP
rm -rf ./${SET_ID}dbsvrd/*.STAT

rm -rf ./${SET_ID}logsvrd/core*
rm -rf ./${SET_ID}logsvrd/*.MMAP
rm -rf ./${SET_ID}logsvrd/*.STAT

rm -rf ./${SET_ID}trfsvrd/core*
rm -rf ./${SET_ID}trfsvrd/*.MMAP
rm -rf ./${SET_ID}trfsvrd/*.STAT

rm -rf ./${SET_ID}gmsvrd/core*
rm -rf ./${SET_ID}gmsvrd/*.MMAP
rm -rf ./${SET_ID}gmsvrd/*.STAT


./make_big_set_mmap_ln.sh 
cd /home/qie_cwar_${PATH_ID}/code/run

svr_list=`echo /home/qie_cwar_${PATH_ID}/code/run/{${SET_ID}dbsvrd,${SET_ID}logsvrd,${SET_ID}trfsvrd,${SET_ID}gmsvrd}`

red="\033[31m"
green="\033[32m"
yellow="\033[33m"
no="\033[0m"


start()
{ 
list=$1
for i in $list
do
   cd $i
   j=`echo ${i##*/}`
   m=$j

   if [ -s ${m}.pid ]
   then
      read SVC_PID < ${m}.pid
   else
      read SVC_PID <<< 0
   fi
   if [ -s ${SET_ID}commsvrd.pid ]
   then
      read com < ${SET_ID}commsvrd.pid
   else
     read com <<< 0
   fi
 
   if [ $com = 0 ]
   then
      ${SET_ID}commsvrd $j &> /dev/null
      if [ $? = 0 ]
      then 
         #rm -f *.MMAP core.*
         sleep 1
         read commsvrd < ${SET_ID}commsvrd.pid
         echo -e "$green $(date) $j ${SET_ID}commsvrd start PID: $commsvrd $no" | tee -a $i/${SET_ID}commsvrd.log 
      else 
         echo -e "$red $(date) $j ${SET_ID}commsvrd start: failed $no" | tee -a $i/${SET_ID}commsvrd.log
         exit
      fi
   else
      kill -0 $com &> /dev/null 
      if [ $? = 0 ]
      then
         echo -e "$yellow $(date) $j $m is running $no" | tee -a $i/${SET_ID}commsvrd.log
         exit
      else
         ${SET_ID}commsvrd $j &> /dev/null
         if [ $? = 0 ]
         then
            #rm -f *.MMAP core.*
            sleep 1
            read commsvrd < ${SET_ID}commsvrd.pid
            echo -e "$green $(date) $j ${SET_ID}commsvrd start PID: $commsvrd $no" | tee -a $i/${SET_ID}commsvrd.log
         else
            echo -e "$red $(date) $j ${SET_ID}commsvrd start: failed $no" | tee -a $i/${SET_ID}commsvrd.log
            exit
         fi   
      fi
   fi
   if [ $SVC_PID = 0 ] 
   then  
      $m $j &> /dev/null
      if [ $? = 0 ]
      then 
         sleep 1
         read mpid < $m.pid
         echo -e "$green $(date) $j $m start PID: $mpid $no"  | tee -a $i/$m.log 
      else
         echo -e "$red $(date) $j $m start: failed $no" | tee -a $i/$m.log
         exit
      fi
   else
      kill -0  $com &> /dev/null
      if [ $? = 0 ]
      then
         echo -e "$yellow $(date) $j $m is running $no" | tee -a $i/$m.log
         exit
      else
         $m $j &> /dev/null
         if [ $? = 0 ]
         then
            sleep 1
            read mpid < $m.pid
            echo -e "$green $(date) $j  start PID: $mpid $no" | tee -a $i/$m.log
         else
            echo -e "$red $(date) $j $m start: failed $no" | tee -a $i/$m.log
            exit
         fi
      fi
   fi
done
}

#sudo chcon -t texrel_shlib_t /home/qie_cwar_${PATH_ID}/code/bin/libprotolib.so

start "$svr_list"


#================

echo "start all svrd finish,good luck"

/home/qie_cwar_${PATH_ID}/code/run/mon_big_set_svr.sh & > /dev/null

echo "start mon end,start all svrd finish"

